package plus.easydo.server.user.mp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.easydo.core.result.R;
import plus.easydo.server.user.mp.constant.UserConstants;
import plus.easydo.server.user.mp.dto.RoleDto;
import plus.easydo.server.user.mp.entity.SysRole;
import plus.easydo.server.user.mp.qo.RoleQo;
import plus.easydo.server.user.mp.service.ISysRoleService;
import plus.easydo.server.user.mp.vo.RoleVo;
import plus.easydo.starter.log.annotation.SystemLog;
import plus.easydo.starter.mybatis.plus.base.MPBaseController;
import plus.easydo.starter.mybatis.plus.result.MPDataResult;
import plus.easydo.starter.oauth.core.utils.Oauth2Utils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色信息
 *
 * @author ruoyi
 */
@Api("角色信息")
@RestController
@RequestMapping("/role")
public class SysRoleController extends MPBaseController {

    @Resource
    private ISysRoleService roleService;

    @PreAuthorize("hasPermission('system:role:list')")
    @PostMapping("/page")
    public MPDataResult list(RoleQo qo) {
        IPage<RoleVo> page = roleService.page(qo);
        return ok(page);
    }

//    @PreAuthorize("hasPermission('system:role:export)")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysRole role) throws IOException
//    {
//        List<SysRole> list = roleService.selectRoleList(role);
//        ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
//        util.exportExcel(response, list, "角色数据");
//    }

    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("hasPermission('system:role:query')")
    @GetMapping(value = "/{roleId}")
    public R<RoleVo> getInfo(@PathVariable Long roleId) {
        return ok(roleService.selectRoleById(roleId));
    }

    /**
     * 新增角色
     */
    @SystemLog(value = "新增角色",saveLog = true)
    @PreAuthorize("hasPermission('system:role:add')")
    @PostMapping
    public R<Object> add(@Validated @RequestBody RoleDto dto) {
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(dto))) {
            return fail("新增角色'" + dto.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(dto))) {
            return fail("新增角色'" + dto.getRoleName() + "'失败，角色权限已存在");
        }
        return opResult(roleService.insertRole(dto));

    }

    /**
     * 修改保存角色
     */
    @SystemLog(value = "修改保存角色",saveLog = true)
    @PreAuthorize("hasPermission('system:role:edit')")
    @PutMapping
    public R<Object> edit(@Validated @RequestBody RoleDto dto) {
        roleService.checkRoleAllowed(dto);
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(dto))) {
            return fail("修改角色'" + dto.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(dto))) {
            return fail("修改角色'" + dto.getRoleName() + "'失败，角色权限已存在");
        }
        return opResult(roleService.updateRole(dto));
    }

    /**
     * 修改保存数据权限
     */
    @SystemLog(value = "修改保存角色数据权限",saveLog = true)
    @PreAuthorize("hasPermission('system:role:edit')")
    @PutMapping("/dataScope")
    public R<Object> dataScope(@RequestBody RoleDto role) {
        roleService.checkRoleAllowed(role);
        return opResult(roleService.authDataScope(role));
    }

    /**
     * 状态修改
     */
    @SystemLog(value = "角色状态修改",saveLog = true)
    @PreAuthorize("hasPermission('system:role:edit')")
    @PutMapping("/changeStatus")
    public R<Object> changeStatus(@RequestBody RoleDto role) {
        roleService.checkRoleAllowed(role);
        role.setUpdateBy(Oauth2Utils.getUserName());
        return opResult(roleService.updateRoleStatus(role));
    }

    /**
     * 删除角色
     */
    @SystemLog(value = "删除角色",saveLog = true)
    @PreAuthorize("hasPermission('system:role:remove')")
    @DeleteMapping("/{roleIds}")
    public R<Object> remove(@PathVariable Long[] roleIds) {
        return opResult(roleService.deleteRoleByIds(roleIds));
    }

    /**
     * 获取角色选择框列表
     */
    @PreAuthorize("hasPermission('system:role:query')")
    @GetMapping("/optionselect")
    public R<List<SysRole>> optionselect() {
        return ok(roleService.selectRoleAll());
    }
}
